limsPurchase.searchPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-purchase-search-list-grid',
            url: limsPurchase.baseUrl + '/queries/searchable',
            service: 'limsPurchaseServiceImpl',
            dbTable: 'T_LIMS_PURCHASE',
            columns: limsPurchase.getShowListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    // _this.refreshTabs(null)
                }
            },
            onCellClick: function (field, row) {
                field === 'applyCode' && _this.loadDetailPage(row)
            },
            onRowActive: function (index, row) {
                // _this.refreshTabs(row)
            },
            page: true,
        }
    },

    loadDetailPage: function (row) {
        workspace.window.load(limsPurchase.baseUrl + '/page/detail' + Gikam.param({
            id: row.id,
            step: 'search'
        }));
    },

    refreshTabs: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.auditRecordGrid).done(function (comp) {
            if (row && row.id) {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_PURCHASE&id=' + row.id
                })
            } else {
                comp.refresh({
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_PURCHASE&id=' + 0
                })
            }
        })
        Gikam.getAsyncComp('lims-purchase-search-list-material-grid').done(function (grid) {
            if (row) {
                grid.refresh({
                    url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials/queries', {
                        id: row.id
                    })
                });
            } else {
                grid.refresh({
                    url: Gikam.printf(limsPurchase.baseUrl + '/{id}/materials/queries', {
                        id: -1
                    })
                });
            }
        });
        _this.refreshAttachments(_this.idManager.trainRecordGrid)
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId)
        let actRow = grid.getActivedRow()
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable, readonly) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            genericQuery: false,
            readonly: readonly,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    getAuditRecordGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.auditRecordGrid,
            columns: limsPurchase.getFlowListColumns()
        }
    },

    getMaterialGrid: function () {
        var _this = this;
        return {
            title: 'BASEMODULE.BASEDATA.PURCHASES.TAB.MATERIAL',
            type: 'grid',
            id: 'lims-purchase-search-list-material-grid',
            service: 'limsPurchaseMaterialServiceImpl',
            dbTable: 'T_LIMS_PURCHASE_MATERIAL',
            deleteFormatter: function (row) {
                return row.id;
            },
            columns: limsPurchase.getMaterialShowListColumns()
        };
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-purchase-search-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getGridParam()]
            }
        });
    },

    idManager: {
        trainRecordGrid: 'lims-purchase-search-list-grid',
        auditRecordGrid: 'lims-purchase-search-list-grid' + '-record',
    },

    init: function () {
        this.create();
    }
};
